﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ADOX;

namespace nhCommon
{
    public class NhAccess
    {
        /// <summary>
        /// 检测accdb数据库是否存在，存在则建立
        /// </summary>
        /// <param name="fullName">全文件名</param>
        /// <returns>有数据库为true；无为false</returns>
        public static bool CrateAccdb(string fullName)
        {
            bool ret = true;
            if (!File.Exists(fullName))
            {
                Catalog cat = new Catalog();
                try
                {
                    cat.Create($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={fullName};");
                    ret = true;
                }
                catch (Exception ex)
                {
                    ret = false;
                    Debug.WriteLine(ex.Message);
                }
            }

            return ret;
        }

        /// <summary>
        /// 读对象属性值到GroupBox中的控件中，对象属性名与控件tag相同
        /// 写GroupBox中的控件内容到对象属性，对象属性名与控件tag相同
        /// </summary>
        /// <param name="control">From或GroupBox</param>
        /// <param name="o">保存数据的对象</param>
        public static void WriteCortToObject(Control control, Object o)
        {
            if (control is Form)
            {
                foreach (Control gb in control.Controls)
                {
                    WriteCortToObject(gb, o);
                }
            }
            else if (control is GroupBox)
            {
                foreach (Control c in control.Controls)
                {
                    string tag = c.Tag as string;
                    if (tag == string.Empty || !c.Enabled) { continue; }

                    if (c is TextBox)
                    {
                        try
                        {
                            PropertyInfo p = o.GetType().GetProperty(tag);

                            if (p.PropertyType == typeof(string))
                            {
                                p.SetValue(o, c.Text);
                            }
                            else if (p.PropertyType == typeof(double))
                            {
                                p.SetValue(o, double.Parse(c.Text));
                            }

                        }
                        catch (Exception)
                        {
                            MessageBox.Show($"{c.Name}.Text转换{o.GetType().Name}.{tag}失败。");
                        }
                    }
                }
            }
        }

        public static void ReadObjectToCort(Control control, Object o)
        {
            if (control is Form)
            {
                foreach (Control gb in control.Controls)
                {
                    ReadObjectToCort(gb, o);
                }
            }
            else if (control is GroupBox)
            {
                foreach (Control c in control.Controls)
                {
                    string tag = c.Tag as string;
                    if (tag == string.Empty || !c.Enabled) { continue; }

                    if (c is TextBox)
                    {
                        try
                        {
                            PropertyInfo p = o.GetType().GetProperty(tag);
                            if (p.PropertyType == typeof(string))
                            {
                                c.Text = NhDb.DbToValue<string>(p.GetValue(o));
                            }
                            else if (p.PropertyType == typeof(double))
                            {
                                c.Text = NhDb.DbToValue<double>(p.GetValue(o)).ToString();
                            }

                        }
                        catch (Exception)
                        {
                            MessageBox.Show($"{tag}转换{c.Name}.Text失败。");
                        }
                    }
                }
            }
        }

        //public static void ReadSettingToCort(Control control, Object o)
        //{
        //    if (control is Form)
        //    {
        //        foreach (Control gb in control.Controls)
        //        {
        //            ReadSettingToCort(gb, o);
        //        }
        //    }
        //    else if (control is GroupBox)
        //    {
        //        foreach (Control c in control.Controls)
        //        {
        //            string tag = c.Tag as string;
        //            if (tag == string.Empty) { continue; }

        //            if (c is TextBox || c is ComboBox)
        //            {
        //                try
        //                {
        //                    PropertyInfo key = o.GetType().GetProperty("SKey");
        //                    PropertyInfo Value = o.GetType().GetProperty("SValue");
        //                    if (p.PropertyType == typeof(string))
        //                    {
        //                        c.Text = NhDb.DbToValue<string>(p.GetValue(o));
        //                    }
        //                    else if (p.PropertyType == typeof(double))
        //                    {
        //                        c.Text = NhDb.DbToValue<double>(p.GetValue(o)).ToString();
        //                    }

        //                }
        //                catch (Exception)
        //                {
        //                    MessageBox.Show($"{tag}转换{c.Name}.Text失败。");
        //                }
        //            }
        //        }
        //    }
        //}
    }
}
